package com.simple.javatraining.testing.testinside.questions;

import com.simple.javatraining.testing.io.InputOutput;
import com.simple.javatraining.testing.testinside.answers.Answer;
import com.simple.javatraining.testing.testinside.answers.ProposedAnswer;

import java.util.List;

public class QuestionWithAnswerWithinRange extends Question{
    private double delta;
    public QuestionWithAnswerWithinRange(String question,
                                         List<ProposedAnswer> answers,
                                         double delta) {
        super(question, answers);
        this.delta= delta;
    }

    public double checkUserAnswerForCorrect(Answer userAnswer) {
        double chosenValue = (double)InputOutput.convertStringToInt(userAnswer.getTextOfAnswer());
        double correspondenceTruth = 0d;
        double correctValue = getCorrectValue();
        if(Math.abs(chosenValue - correctValue) >= delta) {
            return correspondenceTruth;
        } else {
            correspondenceTruth = 1 - Math.abs(chosenValue - correctValue) / delta;
        }
        return correspondenceTruth;
    }

    private double getCorrectValue() {
        List<ProposedAnswer> variantsOfAnswer = this.getVariantsOfAnswer();
        String correctAnswerAsString = null;
        for (ProposedAnswer currentAnswer:variantsOfAnswer) {
            if(currentAnswer.isCorrect()) {
                correctAnswerAsString = currentAnswer.getTextOfAnswer();
                return (double)InputOutput.convertStringToInt(correctAnswerAsString);
            }
        }
        return 0d;
    }

    public void proposeVariantsOfAnswer() {}

    public void printHint() {
        InputOutput.outputMessageToConsole("Введите ответ:");
    }
}
